using Game.Core;
using Game.Main.Model;
using Game.Manager;
using Game.XH;
using SimpleJSON;
using UnityEngine;

namespace Game.Component
{
	public class ClosestoolItem : TrashcanItem
	{
		public enum State
		{
			Close = 0,
			Open = 1
		}

		private State state;

		public override void Init(ItemArchiveData data)
		{
			base.Init(data);
			SetState((State)data.state);
		}

		protected override void LateUpdate()
		{
			if (anim != 0 && anim == Anim.Shake)
			{
				animTimer += Time.deltaTime;
				if (animTimer <= 0.1f)
				{
					model.SetLocalScaleXY(Mathf.Lerp(1f, 1.1f, animTimer * 10f), Mathf.Lerp(1f, 0.9f, animTimer * 10f));
					return;
				}
				if (animTimer <= 0.2f)
				{
					model.SetLocalScaleXY(Mathf.Lerp(1.1f, 1f, (animTimer - 0.1f) * 10f), Mathf.Lerp(0.9f, 1f, (animTimer - 0.1f) * 10f));
					return;
				}
				model.SetLocalScaleXY(1f, 1f);
				anim = Anim.Static;
			}
		}

		public override void QuicklyTouch(TouchEventData touchEventData)
		{
			if (state == State.Close)
			{
				SetState(State.Open);
			}
			else if (state == State.Open)
			{
				SetState(State.Close);
			}
			Singleton<AudioManager>.Instance.PlaySound("sound_closestool_open_close");
		}

		private void SetState(State val)
		{
			if (state != val)
			{
				state = val;
				Transform transform = null;
				Transform transform2 = null;
				if (state == State.Close)
				{
					transform = model.Find("Open");
					transform2 = model.Find("Close");
				}
				else if (state == State.Open)
				{
					transform = model.Find("Close");
					transform2 = model.Find("Open");
				}
				if (transform != null && transform2 != null)
				{
					transform.SetObjectActive(false);
					transform2.SetObjectActive(true);
					BoxCollider2D component = transform2.GetComponent<BoxCollider2D>();
					coll.offset = component.offset + (Vector2)transform2.localPosition;
					coll.size = component.size;
				}
			}
		}

		public override bool CanDestroyItem(BaseItem item)
		{
			if (state == State.Open)
			{
				return base.CanDestroyItem(item);
			}
			return false;
		}

		public override void PackData(JSONObject obj)
		{
			base.PackData(obj);
			obj["33"] = (int)state;
		}
	}
}
